//
//  TravelViewController.m
//  XhlTrip
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TravelViewController.h"
#import "TravelViewCell.h"
#import "RotateView.h"
#import "CusNaviView.h"
#import "TravelModel.h"
#import "UIImageView+WebCache.h"
#import "TravelDetailController.h"
#import "LeaonWorking.h"
#import "MJTableView.h"
#import "NightSingleton.h"
#import "MBProgressHUD.h"
#import "RotateViewController.h"



NSInteger i = 2;
@interface TravelViewController ()<UITableViewDataSource, UITableViewDelegate, MJTableViewDelegate>
@property (nonatomic, retain) NSMutableArray *rotateArray;
@property (nonatomic, retain) RotateView *rotateView;
@property (nonatomic, retain) MJTableView *tableView;
@property (nonatomic, retain) NSMutableArray *travelArr;

@end

@implementation TravelViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.rotateArray = [NSMutableArray array];
        self.travelArr = [NSMutableArray array];
        [self handdata];
    }
    return self;
}





#pragma mark - 网络请求
- (void)handdata
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:hud];
    hud.labelText = @"正在加载";
    [hud show:YES];
    
        /* 轮播图的网络请求 */
    [LeaonWorking netWorkingHanderGetGataWithUrl:@"https://chanyouji.com/api/adverts.json?name=app_featured_banner_android" pathname:@"row" resultBlock:^(id result) {

        [self.rotateArray addObjectsFromArray:result];
        [self createRotateView];
    }];
    
    
    /* 下面的tableview的网络请求 */
    [LeaonWorking netWorkingHanderGetGataWithUrl:@"https://chanyouji.com/api/trips/featured.json?page=1" pathname:@"travel" resultBlock:^(id result) {
        [self.travelArr addObjectsFromArray:result];
        
        /* tableview 重新加载数据 */
        [_tableView reloadData];
        [hud removeFromSuperview];
        [hud release];
    }];
}




- (void)viewDidLoad {
   
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = NO;
   
    self.view.backgroundColor = [UIColor whiteColor];
    /* 调用tableview */
    [self createTableView];
    
    
}


- (void)changeNaviBar
{
#pragma mark - 头部标题栏navigationBar
    
    self.navigationController.navigationBar.translucent = NO;
    CusNaviView *titleview = [[CusNaviView alloc] initWithFrame:CGRectMake(10 * Width, 0, self.navigationController.navigationBar.frame.size.width - 20 * Width, self.navigationController.navigationBar.frame.size.height)];
       self.navigationItem.titleView = titleview;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"view.png"]forBarMetrics:UIBarMetricsDefault];
    [titleview release];
   

}

#pragma mark - 轮播图
- (void)createRotateView
{
    self.rotateView.rotateArr = self.rotateArray;
    [self.rotateView createScroller];
    self.rotateView.block = ^(NSString *str){
        RotateViewController *rotate = [[RotateViewController alloc] init];
        [rotate passDetailValue:str];
        [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
        [self.navigationController pushViewController:rotate animated:YES];
    };
    
}



#pragma mark - 创建tableview
- (void)createTableView
{
    self.tableView = [[MJTableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain delegate:self];
   
   
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 210 * Height;
    self.rotateView = [[[RotateView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 170 * Height)] autorelease];
    self.tableView.tableHeaderView = self.rotateView;
    [self.view addSubview:_tableView];
    [_tableView release];
}





- (void)refreshingHeader:(id)tableView {
    NSLog(@"下拉刷新");

    /* 下面的tableview的网络请求 */
    [LeaonWorking netWorkingHanderGetGataWithUrl:@"https://chanyouji.com/api/trips/featured.json?page=1" pathname:@"travel" resultBlock:^(id result) {
        
        [self.travelArr removeAllObjects];
        [self.travelArr addObjectsFromArray:result];
        
        /* tableview 重新加载数据 */
        [_tableView reloadData];
    }];

}

- (void)refreshingFooter:(id)tableView {
    NSLog(@"上啦加载");
    
    NSString *page = [NSString stringWithFormat:@"%ld", (long)i];
    NSString *str = [NSString stringWithFormat:@"%@%@", @"https://chanyouji.com/api/trips/featured.json?page=", page];
    NSString *ss = [NSString stringWithFormat:@"%@%@", @"travel", page];
    [LeaonWorking netWorkingHanderGetGataWithUrl:str pathname:ss resultBlock:^(id result) {
        [self.travelArr addObjectsFromArray:result];
        
        /* tableview 重新加载数据 */
        [_tableView reloadData];
    }];

    i = i + 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"rr";
    TravelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[TravelViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.SmallImageView sd_setImageWithURL:[[self.travelArr[indexPath.row] objectForKey:@"user"] objectForKey:@"image"]];

    
    TravelModel *model = [[TravelModel alloc] init];
    [model setValuesForKeysWithDictionary:self.travelArr[indexPath.row]];
    cell.travelModel = model;
    [model release];
    
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.travelArr.count;
}


#pragma mark - 点击cell进入详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravelDetailController *travelDetail = [[TravelDetailController alloc] init];
    [travelDetail passDetailValue:[NSString stringWithFormat:@"%@", [self.travelArr[indexPath.row] objectForKey:@"id"]]];
    [self presentViewController:travelDetail animated:YES completion:^{
        
    }];
    [travelDetail release];
}



- (void)dealloc
{
    [_travelArr release];
    [_tableView release];
    [_rotateView release];
    [_rotateArray release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self changeNaviBar];
   
    self.rotateView.scrollerView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
}


@end
